//
//  NetworkManager.h
//  SameWorld
//
//  Created by 李小朋 on 2017/3/16.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "NetworkManager.h"
#import "AFNetworking.h"
#import "YXReachability.h"
@implementation NetworkManager

+ (NetworkManager *)shareManager {
    static NetworkManager *manage;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manage = [[NetworkManager alloc] init];
    });
    return manage;
}

+ (void)requestGETWithURLStr:(NSString *)urlStr paramDic:(NSDictionary *)paramDic finish:(void (^)(id))finish enError:(void (^)(NSError *))enError {
    //创建一个sessionManager管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //AFNetworking请求结果回调时，failure方法会在两种情况下回调：1、请求服务器失败，服务器返回失败信息；2、服务器返回数据成功，AFN解析返回的数据失败
    //指定我们能够解析的数据类型包含html
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain", nil];
    [manager GET:urlStr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finish(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        enError(error);
    }];
}
+ (void)requestPOSTWithURLStr:(NSString *)urlSr paramDic:(NSDictionary *)paramDic finish:(void (^)(id))finish enError:(void (^)(NSError *))enError {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
   manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"application/json",@"text/plain",@"charset=utf-8", @"application/x-javascript", nil];
    [manager POST:urlSr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finish(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        enError(error);
    }];
}

// manager.responseSerializer = [AFHTTPResponseSerializer serializer];  请求返回的是数组需转码Data
+ (void)requestPOSTWithURLStre:(NSString *)urlSr paramDic:(NSDictionary *)paramDic finish:(void (^)(id))finish enError:(void (^)(NSError *))enError {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain", nil];
    [manager POST:urlSr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finish(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        enError(error);
    }];
}



#pragma mark - 带有网络监测的方法
+ (void)requestStatusPOSTWithURLStr:(NSString *)urlSr paramDic:(NSDictionary *)paramDic finish:(void(^)(id responseObject))finish enError:(void(^)(NSError *error))enError {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [self reachabilityStatus];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"text/html",@"application/json",@"text/plain", nil];
    [manager POST:urlSr parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        finish(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        enError(error);
    }];
    

    
  
}

+ (void)reachabilityStatus {

    [YXReachability LGwithSuccessBlock:^(NSString *status) {
        NSLog(@"网络状态%@",status);
        if ([status isEqualToString:@"网络异常，请检查您的网络！"]) {
            [SVProgressHUD showErrorWithStatus:@"网络异常，请检查您的网络！"];
        }
    }];
}


@end
